<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Pgwatch2 :: Metrics and preset configs</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
</head>
<body>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="/index"><h4>PgWatch2</h4></a></li>
        <li><a href="https://www.cybertec-postgresql.com/en/"><img src="/static/logo.png" alt="Cybertec" height="35px"></a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="/dbs">DBs</a></li>
        <li><a href="#">|</a></li>
        <li class="active"><a href="/metrics">Metrics<span class="sr-only"></span></a></li>
        {% if not no_component_logs %}
        <li><a href="#">|</a></li>
        <li class="dropdown" title="Logs will open in a new window">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Logs<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="/logs/pgwatch2/200" target="_blank">Pgwatch2 [last 200 lines]</a></li>
            <li><a href="/logs/influxdb/200" target="_blank">InfluxDB [last 200 lines]</a></li>
            <li><a href="/logs/grafana/200" target="_blank">Grafana [last 200 lines]</a></li>
            <li><a href="/logs/postgres/200" target="_blank">Postgres [last 200 lines]</a></li>
            <li><a href="/logs/webui/200" target="_blank">Web UI [last 200 lines]</a></li>
            <li><a href="/versions" target="_blank">Component versions</a></li>
          </ul>
        </li>
        {% endif %}
        {% if no_anonymous_access and session['logged_in'] %}
        <li><a href="#">|</a></li>
        <li><a href="/logout">Log out</a></li>
        {% endif %}
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>




<div class="container-fluid">

{% if messages %}
    {% for message in messages: %}
    <div class="alert alert-warning alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      {{message}}
    </div>
    {% endfor %}
{% endif %}

<h2>Preset configs</h2>

<div class="table-responsive">
    <table class="table table-striped">
        <thead>
            <tr>
                <td>Name</td>
                <td>Description</td>
                <td>Config JSON</td>
                <td>Active DBs using config</td>
                <td>Last modified</td>
                <td></td>
                <td></td>
            </tr>
        </thead>
        <tbody>
            {% for row in preset_configs: %}
            <tr>
                <form action="/metrics" method="post">
                    <td id="{{row['pc_name']|e}}">{{row['pc_name']|e}}<input type="hidden" name="pc_name" value="{{row['pc_name']}}"></td>
                    <td><textarea name="pc_description" cols="50">{{row['pc_description']|e}}</textarea></td>
                    <td><textarea name="pc_config" cols="50">{{row['pc_config']|e}}</textarea></td>
                    <td><a href="/dbs">{{row['active_dbs']}}</a></td>
                    <td>{{row['pc_last_modified_on']}}</td>
                    <td><input type="submit" name="save" value="Save"></td>
                    <td>{% if row['active_dbs'] == '' %}<input type="submit" name="delete" value="Delete">{% endif %}</td>
                </form>
            </tr>
            {% endfor %}
            <tr>
                <form action="/metrics" method="post">
                    <td><input type="text" name="pc_name" value="" size="10"></td>
                    <td><input type="text" name="pc_description" value=""></td>
                    <td><textarea name="pc_config" cols="50"></textarea></td>
                    <td></td>
                    <td></td>
                    <td><input type="submit" name="new" value="New"></td>
                    <td></td>
                </form>
            </tr>
        </tbody>
    </table>

    <h2>Active metrics listing</h2>
    <ul>
    {% for m in metrics_list: %}
        <li style="display: inline"><b>{{m['m_name']}}</b> [ver: {{m['versions']}}]</li>
    {% endfor %}
    </ul>

</div>

<h2 title="Inactive at the bottom">Metric definitions</h2>

<div class="table-responsive">
    <table class="table table-striped">
        <thead>
            <tr>
                <td>Metric</td>
                <td>PG version from</td>
                <td title="NB! 'epoch_ns' column needs to be always present">SQL</td>
                <td>Comment</td>
                <td>Is active?</td>
                <td title="Code for metric fetching helpers. Not to be used for normal metrics gathering">Is helper?</td>
                <td>Last modified</td>
                <td></td>
                <td></td>
            </tr>
        </thead>
        <tbody>
            {% for row in metric_definitions: %}
            <tr>
                <form action="/metrics" method="post">
                    <td>
                        <input type="hidden" name="m_id" value="{{row['m_id']}}">
                        <input type="text" name="m_name" value="{{row['m_name']|e}}">
                    </td>
                    <td><input type="text" name="m_pg_version_from" value="{{row['m_pg_version_from']}}"></td>
                    <td><textarea name="m_sql" cols="50">{{row['m_sql']}}</textarea></td>
                    <td><input type="text" name="m_comment" value="{{row['m_comment']}}"></td>
                    <td><input type="checkbox" name="m_is_active" {% if row['m_is_active'] %}checked{% endif %}></td>
                    <td><input type="checkbox" name="m_is_helper" {% if row['m_is_helper'] %}checked{% endif %}></td>
                    <td>{{row['m_last_modified_on']}}</td>
                    <td><input type="submit" name="metric_save" value="Save"></td>
                    <td><input type="submit" name="metric_delete" value="Delete" class="delete"></td>
                </form>
            </tr>
            {% endfor %}
            <tr>
                <form action="/metrics" method="post">
                    <td><input type="text" name="m_name" value=""></td>
                    <td><input type="text" name="m_pg_version_from" value="9.0"></td>
                    <td><textarea name="m_sql" cols="50">SELECT (extract(epoch from now()) * 1e9)::int8 as epoch_ns, ...</textarea></td>
                    <td><input type="text" name="m_comment" value=""></td>
                    <td><input type="checkbox" name="m_is_active" checked></td>
                    <td><input type="checkbox" name="m_is_helper"></td>
                    <td></td>
                    <td><input type="submit" name="metric_new" value="New"></td>
                    <td></td>
                </form>
            </tr>
        </tbody>
    </table>
    <div class="alert alert-info">
      <strong>NB! </strong>Metric fetching SQL-s always need to have the "epoch_ns" column present. Additionally columns with "tag_" prefix will be
        turned into indexed tags in InfluxDB (fast access) and also "dbname" (DB unique name) column will always be added by the framework
    </div>
</div>

</div>

<script src="/static/jquery-3.1.1.min.js"></script>
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

<script>
    $( document ).ready(function() {
        $(".delete").click(function(e){
            var r = confirm("Delete?");
            if (r == false) {
                e.preventDefault();
            }
        });
    });
</script>

</body>
</html>
